package com.renzhebiao.lkang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.renzhebiao.lkang.comment.Need;
import com.renzhebiao.lkang.entity.UserPO;
import com.renzhebiao.lkang.facade.UserFacade;
import com.renzhebiao.lkang.master.ExecuteResult;
import com.renzhebiao.lkang.page.PaginationDTO;
import com.renzhebiao.lkang.query.UserQuery;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/user")
@Api(tags = "用户操作")
public class UserController {

    @Autowired
    private UserFacade userFacade;

    @ApiOperation(value = "新增用户", httpMethod = "POST", response = ExecuteResult.class, notes = "根据User对象创建用户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(path = "adduser", produces = "application/json")
    public ExecuteResult<Void> addUser(@RequestBody UserPO user) {
        System.out.println(user.toString());
        this.userFacade.add(user);
        return ExecuteResult.ok();
    }

    @ApiOperation(value = "查询用户", httpMethod = "POST", response = ExecuteResult.class, notes = "根据User对象查询用户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(path = "get/user", produces = "application/json")
    public ExecuteResult<?> getUser(@RequestBody PaginationDTO<UserQuery, Void> paging) {

        return ExecuteResult.ok(this.userFacade.getUserByPage(paging));
    }

    @ApiOperation(value = "新增用户", notes = "新增用户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/add")
    @Need(token = true)
    public ExecuteResult<?> add(@RequestBody UserPO po) {
        this.userFacade.add(po);
        return ExecuteResult.ok();
    }

    @ApiOperation(value = "修改用户", notes = "修改用户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/edit")
    @Need(token = true)
    public ExecuteResult<?> edit(@RequestBody UserPO po) {
        this.userFacade.edit(po);
        return ExecuteResult.ok();
    }

    @ApiOperation(value = "删除用户", notes = "删除用户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/delete")
    @Need(token = true)
    public ExecuteResult<?> delete(@RequestBody Long id) {
        this.userFacade.delete(id);
        return ExecuteResult.ok();
    }

    @ApiOperation(value = "查询用户列表", notes = "查询用户列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/list")
    public ExecuteResult<?> getUserList(@RequestBody UserQuery param) {
        return ExecuteResult.ok(this.userFacade.getUserList(param));
    }

    @ApiOperation(value = "查询用户单条", notes = "查询用户单条", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/get")
    public ExecuteResult<?> getUser(@RequestBody Long id) {
        return ExecuteResult.ok(this.userFacade.getUser(id));
    }

    @ApiOperation(value = "分页查询用户", notes = "分页查询用户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping("/page")
    public ExecuteResult<?> getUserByPage(@RequestBody PaginationDTO<UserQuery, Void> paging) {
        return ExecuteResult.ok(this.userFacade.getUserByPage(paging));
    }

}
